package com.example.lenovo.fragmenttest_329zay.model;

import android.telecom.Call;

import com.example.lenovo.recyclerviewtest_329zay.bean.ArticleBean;
import com.example.lenovo.recyclerviewtest_329zay.iface.ArticleListener;
import com.example.lenovo.recyclerviewtest_329zay.iface.Articleiface;
import com.example.lenovo.recyclerviewtest_329zay.service.ArticleService;

import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2017/4/20.
 */

public class ArticleModel implements Articleiface {
    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";

    //构造函数
    public ArticleModel() {
        //使用retrofit--1
        retrofit = new Retrofit.Builder().baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener articleListener) {
        //使用retrofit--2
        ArticleService articleService = retrofit.create(ArticleService.class);

        retrofit2.Call<List<ArticleBean>> call
                = articleService.getResult(mod, page, sessionID);
        //使用Retrofit-3

        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(retrofit2.Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if (response.isSuccessful() && response != null) {
                    articleListener.onResponse(response.body());
                } else
                    articleListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(retrofit2.Call<List<ArticleBean>> call, Throwable t) {
                articleListener.onFail(t.toString());
            }
        });
    }
}

